﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using blackhouse.mojoPortalExtend.Urls;
using blackhouse.NemoBlog.Business;
using blackhouse.WebNemo.Business.Groups;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using mojoPortal.Web;

namespace blackhouse.NemoBlog.Web {
    public class TagExtension : RecordExtension {

        #region Fields

        private SiteSettings currentSite;

        #endregion

        public TagExtension(GroupRecord record) : base(record) {
            this.currentSite = CacheHelper.GetCurrentSiteSettings();
        }

        protected override string GetIdentyficator(GroupRecord record) {
            return record.GetString("tagName");
        }

        public override bool ShowOnRecordsList(GroupRecord record) {
            if (record.IsNull("site"))
                return false;
            else
                return record.GetInt32("site") == this.currentSite.SiteId;
        }

        public override bool ShowOnReferencesRecordsList(GroupRecord record, GroupColumn parentColumn) {
            return this.ShowOnRecordsList(record);
        }

        protected override void RecordChanged(GroupRecord record, RecordChangeType type) {
            NemoBlogTag tag = new NemoBlogTag(record.RecordId);

            if (type == RecordChangeType.Added) {
                tag.TagGuid = Guid.NewGuid();
                tag.SetValue("site", this.currentSite.SiteId);
                tag.Save(false);
            }

            if (type == RecordChangeType.Added || type == RecordChangeType.Updated) {
                TagUrlSupport tagUrl = new TagUrlSupport(tag);
                //if (!String.IsNullOrEmpty(tag.TagUrl))
                //    tagUrl.SetUserNewUrl(tag.TagUrl);
                UrlSupportProvider urlProvider = new UrlSupportProvider(tagUrl);
                urlProvider.OverridePage(this.GetPageForTagPostsListing());
                urlProvider.Save();
            }

            if (type == RecordChangeType.Deleted) {
                FriendlyUrl urlManage = new FriendlyUrl(CacheHelper.GetCurrentSiteSettings().SiteId, record.GetString("url").ReplaceEx("~/", ""));
                if (urlManage.FoundFriendlyUrl)
                    FriendlyUrl.DeleteUrl(urlManage.UrlId);
            }

        }

        private PageSettings GetPageForTagPostsListing() {
            mojoPortalExtend.PageProvider pp = new mojoPortalExtend.PageProvider();
            PageSettings page= pp.FindCmsPageByFunctionality("blog-tags");
            if (page == null) {
                page = this.CreatePageForTagPostsListing();
                pp.SetCmsPageByFunctionality("blog-tags", page.PageId);
            }
            return page;
        }

        private PageSettings CreatePageForTagPostsListing() {
            PageSettings page = new PageSettings();
            PageSettings parentPage = CacheHelper.GetCurrentPage();

            page.ParentId = parentPage.PageId;
            page.ParentGuid = parentPage.PageGuid;
            page.SiteId = parentPage.SiteId;
            page.SiteGuid = parentPage.SiteGuid;
            page.PageOrder = PageSettings.GetNextPageOrder(parentPage.SiteId, parentPage.PageId);
            page.AuthorizedRoles = parentPage.AuthorizedRoles;
            page.EditRoles = parentPage.EditRoles;
            page.CreateChildPageRoles = parentPage.CreateChildPageRoles;
            page.DraftEditOnlyRoles = parentPage.DraftEditOnlyRoles;
            page.CreatedBy = SiteUtils.GetCurrentSiteUser().UserGuid;
            page.CreatedFromIp = SiteUtils.GetIP4Address();
            page.PageName = "Blog";
            page.PageTitle = "Blog";
            page.AllowBrowserCache = parentPage.AllowBrowserCache;
            page.ShowBreadcrumbs = parentPage.ShowBreadcrumbs;
            page.ShowChildPageBreadcrumbs = parentPage.ShowChildPageBreadcrumbs;
            page.ShowHomeCrumb = parentPage.ShowHomeCrumb;
            page.UseUrl = true;
            page.Url = "~/blog-tags-listing";
            page.IsClickable = true;
            page.IncludeInMenu = false;
            page.IncludeInSiteMap = false;
            page.IncludeInSearchMap = false;
            page.Skin = parentPage.Skin;
            page.Save();

            ModuleDefinition def = new ModuleDefinition(new Guid("192F3E76-8D94-46E6-A9B6-F0DAD826B3A7"));
            Module m = new Module();
            m.SiteId = page.SiteId;
            m.SiteGuid = page.SiteGuid;
            m.ModuleDefId = def.ModuleDefId;
            m.FeatureGuid = def.FeatureGuid;
            m.CacheTime = def.DefaultCacheTime;
            m.PageId = page.PageId;
            m.ModuleTitle = "Blog";
            m.PaneName = "contentpane";
            m.CreatedByUserId = SiteUtils.GetCurrentSiteUser().UserId;
            m.ShowTitle = false;
            m.HeadElement = WebConfigSettings.ModuleTitleTag;
            m.Save();
            page.RefreshModules();

            return page;
        }

    }
}